import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Layout',
      component: () => import ('../layout/Layout.vue'),
      redirect:'/home',
      children:[   // 子路由
        {
          path: 'home',
          name: 'Home',
          component: () => import ('../views/HomeView.vue'),
        },
        {
          path: 'detail',
          name: 'Detail',
          component: () => import ('../views/Detail.vue'),
        },
        {
          path: 'newsDetail',
          name: 'NewsDetail',
          component: () => import ('../views/NewsDetail.vue'),
        },
        {
          path: "dynamicSearch",
          name: 'DynamicSearch',
          component: () => import ('../views/DynamicSearch.vue'),
        },
        {
          path: "messages",
          name: 'Messages',
          component: () => import ('../views/Messages.vue'),
        },
        {
          path: "goods",
          name: 'Goods',
          component: () => import ('../views/Goods.vue'),
        },
        {
          path: "friendsPart",
          name: 'FriendsPart',
          component: () => import ('../views/FriendsPart.vue'),
        },
        {
          path: "orders",
          name: 'Orders',
          component: () => import ('../views/Orders.vue'),
        },
        {
          path: 'personCenter',   // 放在框架里的路由不加/, 因为前加上后就变成了了 //personCenter
          name: 'PersonCenter',
          component: () => import('../views/PersonCenter.vue')
        },
        {
          path: 'im',
          name: 'Im',
          component: () => import('../views/im.vue'),
          children:[
            {
              path: '/chat',
              name: 'Chat',
              component: () => import('../components/Chat.vue'),
            },
            {
              path: '/userAdd',
              name: 'UserAdd',
              component: () => import('../components/UserAdd.vue'),
            },
            {
              path: '/call',
              name: 'Call',
              component: () => import('../components/Call.vue'),
              children:[
                {
                  path: '/item',
                  name: 'Item',
                  component: () => import('../components/Item.vue'),
                }
              ]
            },
          ]
        },
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import ('../views/Login.vue')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import ('../views/Register.vue')
    },
    {
      path: '/404',
      name: '/404',  // 重定向到404页面
      component: () => import ('../views/404.vue')
    },
    {
      path: '/:pathMatch(.*)',  // 匹配所有未知路由
      redirect: '/404'
    },

  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 后端鉴权
    if (!to.matched.length) {
      next('/404')
    } else {
      next()
    }
})

export default router
